<template>
  <div class="catTree">
    <el-scrollbar style="height: 100%">
      <el-tree
        v-if="current"
        class="filter-tree"
        ref="tree"
        highlight-current
        :data="categories"
        :expand-on-click-node="false"
        :filter-node-method="filterCategory"
        :props="defaultCategory"
        @node-click="selectCategory"
        node-key="label"
        :current-node-key="current"
        style="margin-top: 20px; height: 520px; overflow-y: auto"
      >
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
          <span class="fr">
            <i
              @click="() => add(data)"
              v-if="data.isadd !== 1"
              class="el-icon-plus"
            ></i
            ><!--增加分组-->
            <!-- 根节点不需要删除和重命名 -->
            <i
              v-if="data.isdel !== 1"
              @click="() => del(data)"
              class="el-icon-delete"
            ></i
            ><!--删除分组-->
            <i @click="() => rename(data)" class="el-icon-edit"></i
            ><!--重命名分组-->
          </span>
        </span>
      </el-tree>
    </el-scrollbar>
    <el-dialog
      :title="dlgTitle"
      :visible="addshow"
      width="30%"
      @close="clickClose"
      :close-on-click-modal="false"
    >
      <el-form :model="dlgFrom" ref="dlgFrom">
        <el-form-item auto="auto">
          <el-input v-model="dlgFrom.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="clickCacel">取 消</el-button>
        <el-button type="primary" @click="clickAdd">{{ dlgButName }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getApicat,
  addApinode,
  delApinode,
  updataCat,
} from "@/network/networks";
export default {
  name: "categorySet",

  mounted() {
    this.cat_type = this.$parent.$parent.$parent.$parent.getCattype();
  },

  data() {
    return {
      cat_type: -1,
      id: 1,
      index: 1,
      createUser: "",
      createTime: "",
      isDisabled: true, //是否禁用
      categories: [], //档案类别列表
      defaultCategory: {
        children: "children",
        label: "name", // 这里的名字要与封装的数据中的节点字段名一样
      },
      addshow: false,
      dlgFrom: {
        name: "",
      },
      dlgButName: "",
      dlgTitle: "",
      dlgpardata: {},
      dlgeditdata: {},
      editNodeid: -1,
      new_id: 0,
      current: '',
    };
  },
  created() {
    // this.getCategory();
  },
  watch: {
    //实时监控搜索类别
    filterText(val) {
      this.$refs.tree.filter(val);
    },

    cat_type(val) {
      this.getCategory();
    },
  },
  methods: {
    //dialog添加
    async clickAdd() {
      if (this.dlgTitle == "添加节点") {
        await this.Nodeadd();
      } else {
        await this.editNode();
      }
      this.clickClose();
    },

    async editNode() {
      if (this.dlgFrom.name == "") {
        this.$message({
          message: "请输入节点名称",
          type: "warning",
        });
        return;
      } else {
        await updataCat({
          type: this.cat_type,
          id: this.editNodeid,
          name: this.dlgFrom.name,
        })
          .then((res) => {
            if (res.code == 1) {
              this.dlgeditdata.name = this.dlgFrom.name;
              this.$message.success(res.msg);
            } else {
              this.$message.error(res.msg);
            }
          })
          .catch((err) => {
            this.$message.error(err);
          });
      }
    },

    //添加节点
    async Nodeadd() {
      if (this.dlgFrom.name == "") {
        this.$message({
          message: "请输入节点名称",
          type: "warning",
        });
        return;
      } else {
        var del;
        var add;
        if (this.dlgpardata.level == 1) {
          del = 0;
          add = 0;
        } else {
          del = 0;
          add = 1;
        }
        const newChild = {
          id: 0,
          name: this.dlgFrom.name,
          parentid: this.dlgpardata.id,
          level: this.dlgpardata.level + 1,
          isdel: del,
          isadd: add,
          cat_type: this.cat_type,
          children: [],
        };
        await this.addNode(newChild);
        newChild.id = this.new_id;

        if (!this.dlgpardata.children) {
          this.$set(this.dlgpardata, "children", []);
        }
        this.dlgpardata.children.push(newChild);
      }
    },

    //dialog取消
    clickCacel() {
      this.clickClose();
    },

    //关闭dialog
    clickClose() {
      this.dlgpardata = {};
      this.dlgFrom.name = "";
      this.addshow = false;
    },

    //添加接口节点
    async addNode(data) {
      // await addNode(data)
      await addApinode(data).then((res) => {
        console.log(res.data);
        this.new_id = res.data;
      });
    },

    // 获取接口分类
    getCategory() {
      getApicat({
        cat_type: this.cat_type,
      }).then((res) => {
        this.categories = res.data;
        this.current = res.data[0].name
        console.log(res.data[0])
        console.log(this.current)
      });
    },
    // 筛选档案类别
    filterCategory(value, data) {
      if (!value) return true;
      return data.categoryName.indexOf(value) !== -1;
    },
    // 选择档案类别
    selectCategory(data) {
      console.log(data)
      this.$parent.$parent.$parent.$parent.setNodeid(data);
    },

    // 新增
    add(data) {
      this.dlgButName = "添 加";
      this.dlgTitle = "添加节点";
      this.dlgpardata = data;
      this.addshow = true;
    },

    // 删除
    del(data) {
      this.$confirm("确认删除该类别？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delApinode({
          id: data.id,
          cat_type: this.cat_type,
        })
          .then((res) => {
            // this.close();
            if (res.code == 1) {
              this.$message.success(res.msg);
              this.getCategory();
            } else {
              this.$message.error(res.msg);
            }
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },

    //重命名
    rename(data) {
      this.dlgTitle = "修改节点";
      this.dlgButName = "修 改";
      this.dlgFrom.name = data.name;
      this.editNodeid = data.id;
      this.dlgeditdata = data;
      this.addshow = true;
    },
  },
};
</script>

<style lang="less" scoped>
// .fr {
//   margin-left: 70px;
// }

.el-scrollbar .el-scrollbar__wrap {
  overflow-x: hidden;
}
.el-tree {
  .el-tree-node {
    min-width: 120%;
    display: inline-block;
  }
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 15px;
    padding-right: 20px;
  }
}
</style>